home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / daemons / lpd / startdaemon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-16  |  2.8 KB  |  134 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of California at Berkeley. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific prior written permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  */
  12.  
  13. #ifndef lint
  14. static char sccsid[] = "@(#)startdaemon.c    5.2 (Berkeley) 5/5/88";
  15. #endif /* not lint */
  16.  
  17. /*
  18.  * Tell the printer daemon that there are new files in the spool directory.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #ifndef sprite
  25. #include <sys/un.h>
  26. #else
  27. #include <sys/file.h>
  28. #endif
  29. #include "lp.local.h"
  30.  
  31. static perr();
  32.  
  33. startdaemon(printer)
  34.     char *printer;
  35. {
  36. #ifndef sprite
  37.         struct sockaddr_un sun;
  38. #endif
  39.     register int s, n;
  40.     char buf[BUFSIZ];
  41.  
  42. #ifdef sprite
  43.     gethostname(buf, sizeof(buf));
  44.     sprintf(pdevName, "/hosts/%s/dev/printer", buf);
  45.     s = open (pdevName, O_WRONLY, 0);
  46.     if (s < 0) {
  47.         perror(pdevName);
  48.         /*
  49.          * the daemon may be dead, so we attempt to start up a new one
  50.          */
  51.          switch (fork()) {
  52.  
  53.          case 0:
  54.             fprintf(stderr, "attempting to restart lpd\n");
  55.             execl("/sprite/daemons.$MACHINE/lpd", "lpd", 0);
  56.         perror("exec failed");
  57.         return 0;
  58.  
  59.          case -1:
  60.             perror("cannot fork");
  61.         return 0;
  62.  
  63.          default:
  64.             /* loop while we wait for the daemon to start up */
  65.             for (n = 60; --n >= 0;) {
  66.             sleep(1);
  67.             if ((s = open (pdevName, O_WRONLY, 0)) >= 0)
  68.             break;
  69.         }
  70.         if (s < 0)
  71.             return 0;
  72.         fprintf(stderr, "lpd restarted\n");
  73.         break;
  74.          }
  75.     }
  76.  
  77.     (void) sprintf(buf, "\1%s\n", printer);
  78.     n = strlen(buf);
  79.     if (write(s, buf, n) != n) {
  80.         perr("write");
  81.         (void) close(s);
  82.         return(0);
  83.     }
  84.     (void) close(s);
  85.     return(1);
  86. #else
  87.     s = socket(AF_UNIX, SOCK_STREAM, 0);
  88.     if (s < 0) {
  89.         perr("socket");
  90.         return(0);
  91.     }
  92.     sun.sun_family = AF_UNIX;
  93.     strcpy(sun.sun_path, SOCKETNAME);
  94.     if (connect(s, &sun, strlen(sun.sun_path) + 2) < 0) {
  95.         perr("connect");
  96.         (void) close(s);
  97.         return(0);
  98.     }
  99.     (void) sprintf(buf, "\1%s\n", printer);
  100.     n = strlen(buf);
  101.     if (write(s, buf, n) != n) {
  102.         perr("write");
  103.         (void) close(s);
  104.         return(0);
  105.     }
  106.     if (read(s, buf, 1) == 1) {
  107.         if (buf[0] == '\0') {        /* everything is OK */
  108.             (void) close(s);
  109.             return(1);
  110.         }
  111.         putchar(buf[0]);
  112.     }
  113.     while ((n = read(s, buf, sizeof(buf))) > 0)
  114.         fwrite(buf, 1, n, stdout);
  115.     (void) close(s);
  116.     return(0);
  117. #endif
  118. }
  119.  
  120. static
  121. perr(msg)
  122.     char *msg;
  123. {
  124.     extern char *name;
  125.     extern int sys_nerr;
  126.     extern char *sys_errlist[];
  127.     extern int errno;
  128.  
  129.     printf("%s: %s: ", name, msg);
  130.     fputs(errno < sys_nerr ? sys_errlist[errno] : "Unknown error" , stdout);
  131.     putchar('\n');
  132. }
  133.  
  134.